package com.link.base.base.approval.service;


import com.link.base.base.approval.dao.mybatis.mapper.ApprovalNodeMapper;
import com.link.base.base.approval.dao.mybatis.mapper.UsersSelectMapper;
import com.link.base.base.approval.model.ApprovalNode;
import com.link.base.base.approval.model.UsersSelect;
import com.link.base.base.user.dao.mybatis.mapper.UserMapper;
import com.link.base.base.user.model.User;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 15:10
 * @version 1.0
 */

@Service
public class UsersSelectServiceImpl extends BasicServiceImpl<UsersSelect> implements UsersSelectService {
    @Resource
    private UsersSelectMapper usersSelectMapper;

    @Resource
    private ApprovalNodeMapper approvalNodeMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 查询用户范围
     * @param  approvalNodeId
     * @param  userId
     * @return List
     * @throws Exception
     */
    @Override
    public List<User> queryUsers(Long approvalNodeId, Long userId) throws Exception {
        UsersSelect record = new UsersSelect();
        List<User> list = new ArrayList<User>();
        try {
            //查询相应节点ID
            ApprovalNode approvalNode = new ApprovalNode();
            approvalNode.setId(approvalNodeId);
            ApprovalNode recordNode = approvalNodeMapper.queryById(approvalNode);
            //查询上级审批人
            User user = new User();
            user.setId(userId);
            User recordUser = userMapper.queryById(user);

            if (recordNode == null) {
                throw new ServiceException("APPROVAL-012！");
            } else {
                //审批流程节点ID
                record.setApprovalNodeId(recordNode.getId());

                //组织范围
                String orgScope = null;
                if (StringUtils.isNotBlank(recordNode.getOrgScope())) {
                    orgScope = recordNode.getOrgScope();
                }
                record.setOrgScope(orgScope);

                //审批类型
                if (StringUtils.isNotBlank(recordNode.getApprovalType())) {
                    String approvalType = recordNode.getApprovalType();
                    record.setApprovalType(approvalType);
                } else {
                    throw new ServiceException("APPROVAL-035");
                }

                //上下级关系
                String positionType = "PositionType";
                if (StringUtils.isNotBlank(recordNode.getRelationship())) {
                    record.setRelationship(recordNode.getRelationship());
                } else {
                    if (positionType.equals(recordNode.getRelationship())) {
                        throw new ServiceException("APPROVAL-036");
                    }
                }
            }

            if (recordUser == null) {
                throw new ServiceException("APPROVAL-037");
            } else {
                //上级审批人或创建人信息
                if (null == recordUser.getPostnId() && null == recordUser.getOrgId()) {
                    throw new ServiceException("APPROVAL-038");
                } else {
                    //上级审批人或创建人ID
                    record.setUserId(recordUser.getId());
                    //上级审批人职位ID
                    record.setPostnId(recordUser.getPostnId());
                    //上级审批人组织ID
                    record.setOrgId(recordUser.getOrgId());
                }

            }

            list = usersSelectMapper.queryUsers(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
        return list;
    }

    /**
     * 分页查询用户列表
     * @param approvalNodeId
     * @param userId
     * @param qps
     * @return List
     * @throws Exception
     */
    @Override
    public List<User> queryUsersPage(Long approvalNodeId, Long userId, QueryParams qps) throws Exception {
        UsersSelect record = new UsersSelect();
        List<User> list;
        try {
            //查询相应节点ID
            ApprovalNode approvalNode = new ApprovalNode();
            approvalNode.setId(approvalNodeId);
            ApprovalNode recordNode = approvalNodeMapper.queryById(approvalNode);
            //查询上级审批人
            User user = new User();
            user.setId(userId);
            User recordUser = userMapper.queryById(user);

            if (recordNode == null) {
                throw new ServiceException("APPROVAL-012");
            } else {
                //审批流程节点ID
                record.setApprovalNodeId(recordNode.getId());

                //组织范围
                if (StringUtils.isNotBlank(recordNode.getOrgScope())) {

                    record.setOrgScope(recordNode.getOrgScope());
                }
                //审批类型
                if (StringUtils.isNotBlank(recordNode.getApprovalType())) {
                    record.setApprovalType(recordNode.getApprovalType());
                } else {
                    throw new ServiceException("APPROVAL-035");
                }

                //上下级关系
                String positionType = "PositionType";
                if (StringUtils.isNotBlank(recordNode.getRelationship())) {
                    record.setRelationship(recordNode.getRelationship());
                } else {
                    if (positionType.equals(recordNode.getRelationship())) {
                        throw new ServiceException("APPROVAL-036");
                    }
                }
            }

            if (recordUser == null) {
                throw new ServiceException("APPROVAL-037");
            } else {
                //上级审批人或创建人信息
                if (null == recordUser.getPostnId() && null == recordUser.getOrgId()) {
                    throw new ServiceException("APPROVAL-038");
                } else {
                    //上级审批人或创建人ID
                    record.setUserId(recordUser.getId());
                    //上级审批人职位ID
                    record.setPostnId(recordUser.getPostnId());
                    //上级审批人组织ID
                    record.setOrgId(recordUser.getOrgId());
                }

            }
            record.setPage(qps.getPage());
            record.setRows(qps.getRows());
            list = usersSelectMapper.queryUsersPage(record);
            qps.setTotal(record.getTotal());
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
        return list;
    }
}
